package com.sk.bilibili.view.activity;

import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.sk.bilibili.R;
import com.sk.bilibili.adapter.pager.main.HomeFragmentAdapter;
import com.sk.bilibili.adapter.pager.member.MemberFragmentAdapter;
import com.sk.bilibili.bean.Focus;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.callBack.AppBarChangeCallBack;
import com.sk.bilibili.callBack.OnAppBarChangeListener;
import com.sk.bilibili.config.GlideApp;
import com.sk.bilibili.databinding.ActivityFlashBinding;
import com.sk.bilibili.databinding.ActivityMemberBinding;
import com.sk.bilibili.databinding.ActivityMemberHeaderBinding;
import com.sk.bilibili.databinding.ActivityMemberTitleBinding;
import com.sk.bilibili.event.MemberEvent;
import com.sk.bilibili.model.activity.FlashModel;
import com.sk.bilibili.model.activity.MemberModel;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.ViewUtils;
import com.sk.bilibili.view.diy.bottomDialog.SpaceBottomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

public class MemberActivity extends BaseActivity<ActivityMemberBinding, MemberModel> implements AppBarChangeCallBack {

    public static void start(Context context, Integer id) {
        Intent intent = new Intent(context, MemberActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void afterSetView() {
        id = (Integer) getIntent().getSerializableExtra("id");
        initView();
        bindEvent();
        loadData();
    }

    private Integer id;
    private SpaceBottomDialog spaceBottomDialog;

    private void loadData() {
        model.loadMember(id);
    }

    private void bindEvent() {
        bind.header.ivBack.setOnClickListener(v->finish());
        bind.title.ivBack.setOnClickListener(v->finish());
        bind.ablShow.addOnOffsetChangedListener(new OnAppBarChangeListener(this));
        bind.header.ivMore.setOnClickListener(v -> spaceBottomDialog.show(getSupportFragmentManager()));
        bind.title.ivMore.setOnClickListener(v -> spaceBottomDialog.show(getSupportFragmentManager()));
        bind.header.ivSearch.setOnClickListener(v->SpaceSearchActivity.start(this,id));
        bind.title.ivSearch.setOnClickListener(v->SpaceSearchActivity.start(this,id));
    }

    private void initView() {
        spaceBottomDialog = new SpaceBottomDialog(id);
        bind.vpShow.setAdapter(new MemberFragmentAdapter(getSupportFragmentManager(), id));
        bind.tlShow.setupWithViewPager(bind.vpShow);
        bind.vpShow.setCurrentItem(2);
        bind.tbShow.setVisibility(View.GONE);
    }

    public void loadMemberSuccess(Member member) {
        initHeader(member);
        initTitle(member);
    }

    private void initTitle(Member member) {
        ActivityMemberTitleBinding title = bind.title;
        title.tvNikeName.setText(member.getNickName());
        ViewUtils.handleFocus(title.tvFocus,member);
        title.tvFocus.setOnClickListener(v->model.switchFocus(id));
    }

    private void initHeader(Member member) {
        ActivityMemberHeaderBinding header = bind.header;
        GlideApp.with(this).load(member.getExtra().getSkin().getSmallUrl()).bg().into(header.ivBg);
        bind.header.avAvatar.setRes(member.getAvatar(), member.getExtra().getPendant());
        header.tvName.setText(member.getNickName());
        if(member.getSex()==0){
            header.ivSex.setVisibility(View.GONE);
        }else {
            header.ivSex.setImageResource(member.getSex()==1? R.drawable.sex_boy:R.drawable.sex_girl);
        }
        header.ivLevel.setImageResource(ViewUtils.getLevelImage(member.getGrade()));
        header.tvSignature.setText(member.getSignature());
        header.tvFocusNum.setText(String.format("%d关注", member.getExtra().getFocusNum()));
        header.tvFansNum.setText(String.format("%d粉丝", member.getFansNum()));
        int state = DataUtils.getMemberId().equals(member.getId())? VISIBLE : View.GONE;
        header.ivChange.setVisibility(state);
        header.tvInfo.setVisibility(state);
        ViewUtils.handleFocus(header.tvFocus,member);
        header.tvFocus.setOnClickListener(v->model.switchFocus(id));
    }

    @Override
    public void onExpanding() {
        bind.tbShow.setVisibility(View.GONE);
    }
// 注意  toolbar遮挡  要整个取消
    @Override
    public void onClose() {
        bind.tbShow.setVisibility(VISIBLE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleMemberEvent(MemberEvent memberEvent){
        switch (memberEvent.getType()){
            case 0:bind.vpShow.setCurrentItem(memberEvent.getIndex());break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    public void switchFocusSuccess(Focus focus) {
        String state=focus==null?"+关注":"取消关注";
        bind.title.tvFocus.setText(state);
        bind.header.tvFocus.setText(state);
    }

}